package co.example.michael.bigpictureloading;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import co.example.michael.application.CustomApplication;
import co.example.michael.tools.PictureAllTools;


public class MainActivity extends Activity {

    private Button pictureButton;

    private Button downloadButton;

    private final static int SELECT_REQUEST_CODE = 10;

    private ImageView imageView;

    private String pictureUrl="http://img.my.csdn.net/uploads/201407/26/1406383248_3693.jpg";

    private final int ADD_PICTURE = 11;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ADD_PICTURE:
                    Bitmap bitmap = PictureAllTools.getBitmapFromMemoryCache(pictureUrl);
                    if (bitmap!=null){
                        imageView.setImageBitmap(bitmap);
                        Log.e("MainActivity:","从内存中取出图片并初始化");
                    }else {
                        bitmap = PictureAllTools.getBitmapFromDisk(pictureUrl);
                        if (bitmap!=null){
                            imageView.setImageBitmap(bitmap);
                            Log.e("MainActivity:","从sd卡中取出图片并初始化");
                        }
                    }
                    break;

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.one_image_view);
        initImageView();
        pictureButton = (Button) findViewById(R.id.one_button);
        pictureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DialogActivity.class);
                startActivityForResult(intent, SELECT_REQUEST_CODE);
            }
        });
//
        downloadButton = (Button)findViewById(R.id.download_button);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               initImageView();
            }
        });
    }

    /**
     * 初始化界面
     */
    private void initImageView(){
        Bitmap bitmap = PictureAllTools.getBitmapFromMemoryCache(pictureUrl);
        if (bitmap == null){
            bitmap = PictureAllTools.getBitmapFromDisk(pictureUrl);
            if (bitmap==null){
                new Thread(){
                    @Override
                    public void run() {
                        PictureAllTools.addBitmapToDiskAndMemory(pictureUrl);
                        Message message = new Message();
                        message.what = ADD_PICTURE;
                        handler.sendMessage(message);
                        Log.e("MainActivity:","要下载图片");
                    }
                }.start();
            }else {
                imageView.setImageBitmap(bitmap);
                Log.e("MainActivity:","没有下载，从sd卡中取出图片");

            }
        }else {
            imageView.setImageBitmap(bitmap);
            Log.e("MainActivity:","没有下载，从内存中取出图片");
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case FinalConstant.ALBUM_RESULT_CODE:
                Uri uri = data.getData();
                String path = PictureAllTools.getPathFromUri(MainActivity.this, uri);
                Log.e("图片的物理路径：", path);

                PictureAllTools.setImageView(imageView,path);

                break;
            case FinalConstant.CAMERA_RESULT_CODE:

                String cameraPath = Environment.getExternalStorageDirectory() + "/Android/data/MichealCamera/temp.jpg";

                Log.e("拍照的图片的路径：",cameraPath);

                PictureAllTools.setImageView(imageView,cameraPath);
                break;
        }
    }

    @Override
    protected void onPause() {
        try{
            CustomApplication.diskLruCache.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        try{
            CustomApplication.diskLruCache.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
